// miniprogram/pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart_list:[],
    all_select: true,
    total: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserCartList();
  },
  getUserCartList() {
    let db = wx.cloud.database();
    let _this = this;
    wx.cloud.callFunction({
      name: 'login',
      success: res => {
        let openid = res.result.openid;
        db.collection('tb_cart_list').where({ _openid: openid }).get({
          success: res => {
            console.log(res);
            _this.setData({
              cart_list: res.data
            })
            _this.getTotalPrice();
          },
          fail: err => {
            console.log(err);
          }
        })
      },
      fail: err => {
        console.error('网络错误')
      }
    })
  },
  getTotalPrice() {
    var all_select = this.data.all_select;
    var cart_list = this.data.cart_list;
    let total = 0;
    if (all_select) {
      for (let i = 0; i < cart_list.length; i++) {
        total += parseFloat(cart_list[i].cart_item_price * cart_list[i].cart_buy_number);
        cart_list[i].cart_is_select = true;
      }
      console.log(cart_list)
      this.setData({
        total: total.toFixed(2),
        cart_list: cart_list,
        pay_num: cart_list.length,
      })
    } else {
      for (let i = 0; i < cart_list.length; i++) {
        cart_list[i].cart_is_select = false;
      }
      this.setData({
        total: total.toFixed(2),
        cart_list: cart_list,
        pay_num: 0
      })
    }
  },
  //单个点击购物
  getSelectList(e) {
    var idx = e.currentTarget.dataset.idx;
    var cr = this.data.cart_list;
    var asl = this.data.all_select;
    var total = 0;
    var j = 0;
    if (cr[idx].cart_is_select) {
      if (asl) {
        this.setData({
          all_select: false
        })
      }
      cr[idx].cart_is_select = !cr[idx].cart_is_select;
    } else {
      cr[idx].cart_is_select = !cr[idx].cart_is_select;
    }
    for (let i = 0; i < cr.length; i++) {
      if (cr[i].cart_is_select) {
        total += parseFloat(cr[i].cart_item_price * cr[i].cart_buy_number);
        j++;
      }
    }
    console.log(cr)
    console.log(j);
    this.setData({
      total: total.toFixed(2),
      cart_list: cr,
      pay_num: j,
    })
  },
  //全选
  getSelectCartList() {
    var all_select = !this.data.all_select;
    this.setData({
      all_select: all_select
    })
    this.getTotalPrice();
  },
  goPayment() {
    let cart_list=this.data.cart_list;
    let buylist=[];
    for(let i=0;i<cart_list.length;i++){
      if (cart_list[i].cart_is_select){
        buylist.push(cart_list[i])
      }
    }
    wx.setStorageSync('buyinfo', buylist);
    wx.navigateTo({
      url: '../payment/payment',
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})